package com.chpro.scsw.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.chpro.scsw.model.User;
import com.chpro.scsw.service.IMenuService;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;

@Controller
@RequestMapping("user")
public class UserAction {
	
	@Autowired
	private IMenuService menuService;
	
	/**
	 * QQ互联登录
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("qq/login")
	public void login(HttpServletRequest request,HttpServletResponse response)throws Exception{
		response.setContentType("text/html;charset=utf-8");
        response.sendRedirect(new Oauth().getAuthorizeURL(request));
	}
	
	/**
	 * QQ互联回调接口
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("qq/callBack")
	public void callBack(HttpServletRequest request,HttpServletResponse response)throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		
		AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);

		String accessToken   = null;
		String openID        = null;
		long tokenExpireIn = 0L;
         
		if (accessTokenObj.getAccessToken().equals("")) {
        	 //我们的网站被CSRF攻击了或者用户取消了授权
        	 //做一些数据统计工作
			System.out.print("没有获取到响应参数");
		} else {
			User user = new User();
			accessToken = accessTokenObj.getAccessToken();
			tokenExpireIn = accessTokenObj.getExpireIn();
           
			user.setAccessToken(accessToken);
           
			//利用获取到的accessToken 去获取当前用的openid -------- start
			OpenID openIDObj =  new OpenID(accessToken);
			openID = openIDObj.getUserOpenID();
           
			UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
			UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
           
			user.setNickName(userInfoBean.getNickname());	//获得昵称
			user.setAvatarUrl(userInfoBean.getAvatar().getAvatarURL30());	//设置头像链接
           
			request.getSession().setAttribute("user", user);	//设置登录
			System.out.println("userInfoBean.getNickname()"+userInfoBean.getNickname());
			System.out.println("userInfoBean.getAvatar().getAvatarURL30()"+userInfoBean.getAvatar().getAvatarURL30());
			response.sendRedirect("http://www.souchisouwan.com");
       }
	}
	
	/**
	 * 用户资料
	 * @return
	 */
	@RequestMapping("profile")
	public ModelAndView profile()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/profile");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 积分明细
	 * @return
	 */
	@RequestMapping("point")
	public ModelAndView point()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/point");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 推广明细
	 * @return
	 */
	@RequestMapping("visit")
	public ModelAndView visit()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/visit");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 我的下线
	 * @return
	 */
	@RequestMapping("reffers")
	public ModelAndView reffers()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/reffers");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 投稿
	 * @return
	 */
	@RequestMapping("contribute")
	public ModelAndView contribute()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/contribute");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
	/**
	 * 申请提现
	 * @return
	 */
	@RequestMapping("withdraw")
	public ModelAndView withdraw()throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/withdraw");
		modelAndView.addObject("menuList", menuService.queryMenuList());
		return modelAndView;
	}
	
}
